package com.engine.salary.enums.common;

import com.engine.salary.enums.BaseEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.function.Predicate;

public enum FilterEnum implements BaseEnum<String> {

    BT("BT", "包含", 1) {
        @Override
        public Predicate<String> filter(List<String> params) {
            return a -> a != null && a.contains(params.get(0));
        }
    },
    EQ("EQ", "等于", 1) {
        @Override
        public Predicate<String> filter(List<String> params) {
            return a -> {
                if (NumberUtils.isCreatable(a) && NumberUtils.isCreatable(params.get(0))) {
                    return new BigDecimal(a).compareTo(new BigDecimal(params.get(0))) == 0;
                } else {
                    return a != null && a.equals(params.get(0));
                }
            };
        }
    },
    NE("NE", "不等于", 1) {
        @Override
        public Predicate<String> filter(List<String> params) {
            return a -> {
                if (NumberUtils.isCreatable(a) && NumberUtils.isCreatable(params.get(0))) {
                    return new BigDecimal(a).compareTo(new BigDecimal(params.get(0))) != 0;
                } else {
                    return a != null && !a.equals(params.get(0));
                }
            };
        }
    },
    GT("GT", "大于", 1) {
        @Override
        public Predicate<String> filter(List<String> params) {
            return a -> {
                if (NumberUtils.isCreatable(a) && NumberUtils.isCreatable(params.get(0))) {
                    return new BigDecimal(a).compareTo(new BigDecimal(params.get(0))) > 0;
                }
                return false;
            };
        }
    },
    LT("LT", "小于", 1) {
        @Override
        public Predicate<String> filter(List<String> params) {
            return a -> {
                if (NumberUtils.isCreatable(a) && NumberUtils.isCreatable(params.get(0))) {
                    return new BigDecimal(a).compareTo(new BigDecimal(params.get(0))) < 0;
                }
                return false;
            };
        }
    },
    GE("GE", "大于等于", 1) {
        @Override
        public Predicate<String> filter(List<String> params) {
            return a -> {
                if (NumberUtils.isCreatable(a) && NumberUtils.isCreatable(params.get(0))) {
                    return new BigDecimal(a).compareTo(new BigDecimal(params.get(0))) >= 0;
                }
                return false;
            };
        }
    },
    LE("LE", "小于等于", 1) {
        @Override
        public Predicate<String> filter(List<String> params) {
            return a -> {
                if (NumberUtils.isCreatable(a) && NumberUtils.isCreatable(params.get(0))) {
                    return new BigDecimal(a).compareTo(new BigDecimal(params.get(0))) <= 0;
                }
                return false;
            };
        }
    },
    ISEMPTY("ISEMPTY", "为空", 1) {
        @Override
        public Predicate<String> filter(List<String> params) {
            return StringUtils::isEmpty;
        }
    },
    ISNOTEMPTY("ISNOTEMPTY", "不为空", 1) {
        @Override
        public Predicate<String> filter(List<String> params) {
            return StringUtils::isNotEmpty;
        }
    };


    FilterEnum(String value, String defaultLabel, int labelId) {
        this.value = value;
        this.defaultLabel = defaultLabel;
        this.labelId = labelId;
    }

    private String value;

    private String defaultLabel;

    private int labelId;

    public abstract Predicate<String> filter(List<String> params);


    @Override
    public String getValue() {
        return value;
    }

    @Override
    public Integer getLabelId() {
        return labelId;
    }

    @Override
    public String getDefaultLabel() {
        return defaultLabel;
    }


}
